package com.hospital.service.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hospital.service.project.entity.help.ProInfoDetailsHelp;
import enhance.plus.logic.LogicBaseServiceImpl;
import com.hospital.service.project.entity.ProInfoDetails;
import com.hospital.service.project.mapper.ProInfoDetailsMapper;
import com.hospital.service.project.service.ProInfoDetailsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.List;

/**
* 实验项目详细表 接口
*
* @author xhl
* @created 2020-10-10 14:59:20
*/
@Slf4j
@Service
public class ProInfoDetailsServiceImpl extends LogicBaseServiceImpl<ProInfoDetailsMapper, ProInfoDetails> implements ProInfoDetailsService{

    /**
     * 添加中间表信息
     *
     * @param projectNumber
     * @param implementerNumbers
     */
    @Override
    public void saveItem(String projectNumber, List<String> implementerNumbers) {
        log.info("开始准备数据");
        int size = implementerNumbers.size();
        List<ProInfoDetails> proInfoDetailsList = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            ProInfoDetails details = new ProInfoDetails();
            details.setImplementerNumber(implementerNumbers.get(i));
            details.setProjectNumber(projectNumber);
            proInfoDetailsList.add(details);
        }
        log.info("数据准备完成，插入数据");
        this.saveBatch(proInfoDetailsList);
    }

    /**
     * 更新此项目的参与者信息
     *
     * @param projectNumber
     * @param implementerNumbers
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateMessage(String projectNumber, List<String> implementerNumbers) {
        log.info("先删除此项目原有的实施人信息");
        UpdateWrapper<ProInfoDetails> updateWrapper = ProInfoDetailsHelp.createUpdate();
        updateWrapper.eq(ProInfoDetailsHelp.projectNumber, projectNumber);
        this.remove(updateWrapper);

        log.info("将现有的实施人添加");
        this.saveItem(projectNumber, implementerNumbers);
    }
}
